package com.ewanghuiju.app.ui.login.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.App;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseActivity;
import com.ewanghuiju.app.base.contract.login.CodeLoginContract;
import com.ewanghuiju.app.model.bean.request.LoginRequestBean;
import com.ewanghuiju.app.model.bean.response.BindUserinfoResponBean;
import com.ewanghuiju.app.model.bean.response.LoginResponBean;
import com.ewanghuiju.app.model.bean.response.UserInfoResponBean;
import com.ewanghuiju.app.model.prefs.ImplPreferencesHelper;
import com.ewanghuiju.app.presenter.login.CodeLoginPresenter;
import com.ewanghuiju.app.ui.main.activity.MainActivity;
import com.ewanghuiju.app.ui.web.MyWebviewActivity;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.StartActivityUtil;
import com.ewanghuiju.app.util.StringUtil;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.ewanghuiju.app.widget.EditTextWithDel;
import com.gyf.immersionbar.ImmersionBar;
import com.muddzdev.styleabletoast.StyleableToast;

import java.util.List;

import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.core.content.ContextCompat;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class 验证码登录页面  请绑定手机号码
 * @time 2020/2/14
 */
public class CodeLoginActivity extends BaseActivity<CodeLoginPresenter> implements CodeLoginContract.View {

    @BindView(R.id.et_phone)
    EditTextWithDel etPhone;
    @BindView(R.id.et_code)
    EditTextWithDel etCode;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.center_view)
    LinearLayout centerView;
    @BindView(R.id.checkbox)
    AppCompatCheckBox checkbox;
    @BindView(R.id.layout_xieyi)
    LinearLayout layoutXieyi;
    @BindView(R.id.tv_regist)
    TextView tvRegist;
    @BindView(R.id.layout_regist)
    LinearLayout layoutRegist;
    @BindView(R.id.login_text)
    TextView loginText;

    private int mType;
    private String unionid, mNickname, mHeadimgurl;

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_code_login;
    }

    @Override
    protected void initEventAndData() {
        mType = getIntent().getIntExtra(Constants.BIND_USERINFO, 1);
        unionid = getIntent().getStringExtra(Constants.BIND_UNIONID);
        mNickname = getIntent().getStringExtra(Constants.BIND_NICKNAME);
        mHeadimgurl = getIntent().getStringExtra(Constants.BIND_HEADIMGURL);

        if (mType == 1) {
            layoutRegist.setVisibility(View.VISIBLE);
            tvLogin.setText("登录");
        } else if (mType == 2) {
            layoutRegist.setVisibility(View.INVISIBLE);
            loginText.setText("请绑定手机号码");
            tvLogin.setText("下一步");
        }
    }

    @OnClick({R.id.tv_login, R.id.tv_code, R.id.tv_regist, R.id.tv_yhxy, R.id.tv_yszc})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.tv_login:
                loginByPhoneCode();
                break;
            case R.id.tv_code:
                //获取验证码
                getCode();
                break;
            case R.id.tv_regist:
                new StartActivityUtil(mContext, PhoneRegistbindActivity.class).putExtra(Constants.LOGIN_TYPE, 1)
                        .startActivity(true);
                break;
            case R.id.tv_yhxy:
                //用户协议
                new StartActivityUtil(mContext, MyWebviewActivity.class).putExtra("url", Constants.WEBURL_USERXY)
                        .putExtra(Constants.WEBURL_TITLE, "用户协议").startActivity(true);
                break;
            case R.id.tv_yszc:
                //隐私政策
                new StartActivityUtil(mContext, MyWebviewActivity.class).putExtra("url", Constants.WEBURL_USERXY)
                        .putExtra(Constants.WEBURL_TITLE, "隐私政策").startActivity(true);
                break;
            default:
                break;
        }
    }

    /**
     * 获取验证码
     */
    private void getCode() {
        if (!checkbox.isChecked()) {
            StyleableToast.makeText(mContext, "请选择用户协议", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        String phoneNum = etPhone.getText().toString();
        if (TextUtils.isEmpty(phoneNum)) {
            StyleableToast.makeText(mContext, "请输入手机号", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        if (!StringUtil.isPhone(phoneNum)) {
            StyleableToast.makeText(mContext, "请输入正确手机号", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        LoadingDialogUtils.show(mContext);
        mPresenter.getCode(phoneNum.trim());
    }

    /**
     * 验证码登录
     */
    private String phoneNum;

    private void loginByPhoneCode() {
        if (!checkbox.isChecked()) {
            StyleableToast.makeText(mContext, "请选择用户协议", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        phoneNum = "";
        phoneNum = etPhone.getText().toString();
        if (TextUtils.isEmpty(phoneNum)) {
            StyleableToast.makeText(mContext, "请输入手机号", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        if (!StringUtil.isPhone(phoneNum)) {
            StyleableToast.makeText(mContext, "请输入正确手机号", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        String codeStr = etCode.getText().toString();
        if (TextUtils.isEmpty(codeStr)) {
            StyleableToast.makeText(mContext, "请输入验证码", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }

        LoadingDialogUtils.show(mContext);
        if (mType == 1) {
            LoginRequestBean loginRequestBean = new LoginRequestBean();
            loginRequestBean.setMobile(phoneNum);
            loginRequestBean.setPwd("");
            loginRequestBean.setType("2");
            loginRequestBean.setCode(codeStr);
            mPresenter.phoneLogin(loginRequestBean);
        } else if (mType == 2) {
            mPresenter.getUserNum(unionid, phoneNum, codeStr);
        }
    }

    @Override
    public void showLogin(LoginResponBean bean) {
        LoadingDialogUtils.dismissDialog_ios();
        if (bean != null && !TextUtils.isEmpty(bean.getIs_bind_parent()) && "1".equals(bean.getIs_bind_parent())) {
            if (!TextUtils.isEmpty(bean.getToken())) {
                ImplPreferencesHelper mImplPreferencesHelper = App.getAppComponent().preferencesHelper();
                mImplPreferencesHelper.setToken(bean.getToken());
                //获取用户信息  到达首页
                LoadingDialogUtils.show(mContext);
                mPresenter.getUserInfo();
            }
        } else {
            //绑定用户信息  去填写邀请码
//            new StartActivityUtil(mContext, AddInvitecodeActivity.class)
//                    .putExtra(Constants.BIND_USERINFO, 2)
//                    .putExtra(Constants.REGIST_PHONE, phoneNum)
//                    .putExtra(Constants.BIND_UNIONID, unionid)
//                    .putExtra(Constants.BIND_NICKNAME, mNickname)
//                    .putExtra(Constants.BIND_HEADIMGURL, mHeadimgurl)
//                    .startActivity(true);
            new StartActivityUtil(mContext, AddInvitecodeActivity.class).putExtra(Constants.REGIST_PHONE, phoneNum).startActivity(true);
        }

    }

    @Override
    public void showLoginResponseMsg(int code, String message) {

    }

    @Override
    public void showUserInfo(UserInfoResponBean bean) {
        //用户信息
        LoadingDialogUtils.dismissDialog_ios();
        StyleableToast.makeText(mContext, "登录成功", Toast.LENGTH_SHORT, R.style.mytoast).show();
        new StartActivityUtil(mContext, MainActivity.class)
                .startActivity(false);
        onBackPressedSupport();
        slideRightIn();
    }

    @Override
    public void showWxUserRegister(LoginResponBean bean) {
        //微信注册成功回调
        LoadingDialogUtils.dismissDialog_ios();
        if (bean != null && !TextUtils.isEmpty(bean.getToken())) {
            ImplPreferencesHelper mImplPreferencesHelper = App.getAppComponent().preferencesHelper();
            mImplPreferencesHelper.setToken(bean.getToken());
            //获取用户信息  到达首页
            LoadingDialogUtils.show(mContext);
            mPresenter.getUserInfo();
        }
    }

    @Override
    public void showCodeText(String text) {
        if (tvCode != null) {
            tvCode.setText(text);
        }
    }

    @Override
    public void showCodeComplete() {
        if (tvCode != null) {
            tvCode.setText("获取验证码");
            tvCode.setClickable(true);
            tvCode.setTextColor(ContextCompat.getColor(mContext, R.color.text_primary));
        }
    }

    @Override
    public void showCodeOn() {
        LoadingDialogUtils.dismissDialog_ios();
        if (tvCode != null) {
            tvCode.setClickable(false);
            tvCode.setTextColor(ContextCompat.getColor(mContext, R.color.color_999));
        }
    }

    @Override
    public void showUserNum(List<BindUserinfoResponBean> list) {
        LoadingDialogUtils.dismissDialog_ios();
        if (list == null || list.size() == 0) {
            //跳转到绑定用户
            new StartActivityUtil(mContext, AddInvitecodeActivity.class)
                    .putExtra(Constants.BIND_USERINFO, 2)
                    .putExtra(Constants.REGIST_PHONE, phoneNum)
                    .putExtra(Constants.BIND_UNIONID, unionid)
                    .putExtra(Constants.BIND_NICKNAME, mNickname)
                    .putExtra(Constants.BIND_HEADIMGURL, mHeadimgurl)
                    .startActivity(true);
//            new StartActivityUtil(mContext, AddInvitecodeActivity.class).putExtra(Constants.REGIST_PHONE, phoneNum).startActivity(true);
        } else {
            BindUserinfoResponBean bindUserinfoResponBean = list.get(0);
            if (TextUtils.isEmpty(bindUserinfoResponBean.getNumber())) {
                new StartActivityUtil(mContext, AddInvitecodeActivity.class)
                        .putExtra(Constants.BIND_USERINFO, 2)
                        .putExtra(Constants.REGIST_PHONE, phoneNum)
                        .putExtra(Constants.BIND_UNIONID, unionid)
                        .putExtra(Constants.BIND_NICKNAME, mNickname)
                        .putExtra(Constants.BIND_HEADIMGURL, mHeadimgurl)
                        .startActivity(true);
            } else {
                LoadingDialogUtils.show(mContext);
                mPresenter.wxUserRegister(unionid, phoneNum, bindUserinfoResponBean.getNumber(), mHeadimgurl, mNickname);
            }
        }
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .titleBar(mToolbar, true)
//                .transparentBar()
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.stopInterval();
        }
    }
}
